<template>
  <el-button size="small" type="primary" icon="el-icon-plus" @click="visibleImportPeij = true">
    导入
    <BaseDialog style="min-height: 80vh;" :visible.sync="visibleImportPeij" width="80vw" title="配件代提报订单批量导入"
                destroyOnClose
                :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '50px' }"
    >
      <ExcelImport
        ref="import"
        template-file-name="配件导入模板.xlsx"
        :importRules="importRules"
        :onComplete="importComplete"
        @close="visibleImportPeij = false"
      />
    </BaseDialog>
  </el-button>
</template>
<script>
import {dictionary} from '@/utils/dictMixin';
import {postImportPartOrders, postValidImportData} from '@/api/partsmanage/purchaseManage/purchaseSubmit';
import {culOrderData, ddlxArr, getPartDetails} from './constant';
import ExcelImport from '@/components/ExcelImport';

export default {
  name: 'OEMImport',
  components: {
    ExcelImport
  },
  props: {
    handleType: {
      type: String,
      default: 'add'
    },
    pageType: {
      type: String,
      default: ''
    },
    value: {
      type: Object,
      default: null
    }
  },
  mixins: [dictionary],
  data() {
    return {
      visibleImportPeij: false,
      importRules: this.initImportRuleFun()
    };
  },
  mounted() {
  },
  methods: {
    initImportRuleFun() {
      return [
        {
          prop: 'dealerCode', title: '经销商代码',
          rule: [
            {required: true, message: '经销商代码不能为空'},
          ]
        },
        {
          prop: 'xsddlx', title: '订单类型',
          rule: [
            {required: true, message: '订单类型不能为空'},
            {itemList: ddlxArr.filter(i => i.value != 'Q')}
          ]
        },
        {
          prop: 'bjxmbh', title: '配件代码',
          rule: [
            {required: true, message: '配件代码不能为空'},
          ]
        },
        {
          prop: 'bjpjsx', title: '配件属性',
          rule: [
            {required: true, message: '配件属性不能为空'},
            {itemList: this.createDictList('5000')}
          ]
        },
        {
          prop: 'bstxsl', title: '采购数量',
          rule: [
            {required: true, message: '采购数量不能为空'},
            {pattern: /(^[1-9]\d*$)/, message: '请填写正确的采购数量'}
          ]
        }
      ]
    },
    //导入
    async importComplete(datas, callback) {
      const errorData = []
      datas.forEach((item, index1) => {
        const indexarr = []
        datas.forEach((i, index2) => {
          if (i.dealerCode === item.dealerCode
            && i.xsddlx === item.xsddlx
            && i.bjxmbh === item.bjxmbh
            && index1 !== index2) {
            indexarr.push(index2 + 1)
          }
        })
        if (indexarr.length) {
          errorData.push({
            rownum: index1 + 1,
            errormsg: `配件信息和${indexarr.join(',')}行重复；`
          })
        }
      })
      if (errorData.length) { //有错误
        callback(errorData)
      } else {
        postValidImportData(datas).then(res => {
          const data = res;
          if (data.success) {
            const orders = data.data.map(i => {
              i.details = getPartDetails(i);
              return culOrderData(i);
            });
            postImportPartOrders(orders).then(res => {
              if (res.resultCode === 200) {
                callback(); //完成
                this.$emit('close', 'refresh');
                this.visibleImportPeij = false;
              } else {
                callback([{errormsg: '批量导入失败！'}]);
              }
            });
          } else {
            callback(data.data);
          }
        }).catch(e => {
          callback([{errormsg: e.errMsg || '服务器运行异常！'}]);
        })
      }
    },
  }
};
</script>

<style lang="less" scoped>
</style>
